import SQLiteStorage from 'react-native-sqlite-storage';
import SQLiteHelper from './SQLiteHelper.js';

export default class StudentInfo {
    constructor() {
        this.student_table_name = 'student_info';
        this.Hepler = new SQLiteHelper();
        this.db = null;
    }

    //id, name, password, actor, login_time
    async createStudentInfoTable() {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => {console.log('ok');}, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }
            this.db.transaction((tx) => {
                tx.executeSql('CREATE TABLE IF NOT EXISTS ' + this.student_table_name + '(' +
                    'id INTEGER PRIMARY KEY NOT NULL,' +
                    'name VARCHAR,' +
                    'password VARCHAR,' +
                    'num VARCHAR,' +
                    'actor VARCHAR,' +
                    'login_time VARCHAR' +
                    ');'
                    , [], () => {
                        resolve();
                    }, (err) => {
                        reject(err);
                    });
            });
        });
    }

    insertStudentInfo(name, password, num, actor) {
        let now = new Date();
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => {console.log('ok');}, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }
            if (this.db) {
                this.db.executeSql(
                    'INSERT INTO ' + this.student_table_name + ' (name,password,num,actor,login_time) VALUES(?,?,?,?,?)',
                    [name, password, num, actor, now.getTime()],
                    () => {
                        resolve();
                    },
                    (err) => {
                        reject(err);
                    });
            } else {
                reject('db not open');
            }
        });
    }

    queryStudentInfo() {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => {console.log('ok');}, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }
            if (this.db) {
                this.db.executeSql('SELECT * FROM ' + this.student_table_name + ' LIMIT 1', [],
                    (results) => {
                        if (results.rows.length > 0) {
                            resolve(results.rows);
                        } else {
                            resolve(null);
                        }
                    }, (err) => {
                        reject(err);
                    });
            } else {
                reject('db not open');
            }
        });
    }

    deleteStudentInfo() {
        return new Promise((resolve, reject) => {
            if (!this.db) {
                this.Hepler.openDatabase().then(() => {console.log('ok');}, (err) => {
                    console.log(err);
                });
                this.db = this.Hepler.db;
            }
            if (!this.db) {
                reject('open db failed');
            }
            if (this.db) {
                this.db.executeSql('DROP TABLE IF EXISTS ' + this.student_table_name, [],
                    (results) => {
                        if (results) {
                            resolve();
                        } else {
                            reject('delete fail');
                        }
                    }, (err) => {
                        reject(err);
                    });
            } else {
                reject('db not open');
            }
        });
    }

    closeStudentInfo() {
        this.Hepler.closeDatabase();
    }
    /*
  try {
    var val1 = await firstStep();
    var val2 = await secondStep(val1);
    var val3 = await thirdStep(val1, val2);

    console.log('Final: ', val3);
  }
  catch (err) {
    console.error(err);
  }
     */
}